<?php
namespace Apipad\Controller;
use Think\Controller;
//用户信息
header("content-type:application/json;charset=utf-8");
class UserController extends ComController{
   //用户详情
   public function user_detail(){
        $uid=UID;
        $model_user=M("user"); 
        $where['issh']=1; 
        $where['uid']=$uid; 
        $rs_systemInfo = $model_user->where($where)->field('customer_no,username,company,mobile,names,avatar')->find();
        $data['code']=200;
        $data['msg']='查询成功';
        $data['data']['info']=$rs_systemInfo;
        exit(json_encode($data));
   }
   public function config_detail(){
        $uid=UID;
        $user_config=M("user_config"); 
        $where['uid']=$uid; 
        $rddo = $user_config->where($where)->find();
        $data['code']=200;
        $data['msg']='查询成功';
        $data['data']['info']=$rddo;
        exit(json_encode($data));
    }
   
      //二维码
    public function qorde_detail(){
        $uid=UID;
        $model_user=M("user"); 
        $where['issh']=1; 
        $where['uid']=$uid; 
        $rs_systemInfo = $model_user->where($where)->field('zhifubao_pay,weixin_pay')->find();
        $data['code']=200;
        $data['msg']='查询成功';
        $data['data']['info']=$rs_systemInfo;
        exit(json_encode($data));
   }
   //更新二维码
   public function qorde_update(){
        $uid=UID;
        $model_user=M("user"); 
        $detail = I('post.');
        $zhifubao_pay=$detail['zhifubao_pay'];
        $weixin_pay=$detail['weixin_pay'];
        if(empty($zhifubao_pay)){
            $data['code']=0;
            $data['msg']='请上传支付宝收款码';
            exit(json_encode($data));
        }
        if(empty($weixin_pay)){
            $data['code']=0;
            $data['msg']='请上传微信收款码';
            exit(json_encode($data));
        }
        $where['issh']=1; 
        $where['uid']=$uid; 
        $save['zhifubao_pay']=$zhifubao_pay;
        $save['weixin_pay']=$weixin_pay;
        $result= $model_user->where($where)->save($save);
        if($result) {
            $data_cc['code'] = 200;
            $data_cc['msg'] = "编辑成功";
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = "编辑失败";
            exit(json_encode($data_cc));
        }
   }

    //更新密码
    public function pass_update(){
        $uid=UID;
        $model_user=M("user"); 
        $detail = I('post.');
        $aOldPwd = $detail["aOldPwd"];
        $aNewPwd = $detail["aNewPwd"];
        if(empty($aOldPwd)){
            $data['code']=0;
            $data['msg']='请输入旧密码';
            exit(json_encode($data));
        }
        if(empty($aNewPwd)){
            $data['code']=0;
            $data['msg']='请输入新密码';
            exit(json_encode($data));
        }
        $where['issh']=1; 
        $where['uid']=$uid; 
        $rs_pwd = $model_user->where($where)->field("password,salt")->find();
        $salt=$rs_pwd['salt'];
        $aOldPwdMd5=$this->getEncryptPassword($aOldPwd,$salt);
        if($aOldPwdMd5 != $rs_pwd["password"]){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "原密码错误";
            exit(json_encode($data_cc));
        }
        if($aOldPwd==$aNewPwd){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "原密码和新密码一致,无法修改";
            exit(json_encode($data_cc));
        }
        $salt2= $this->GetRandStr(6);
        $save["password"] =$this->getEncryptPassword($aNewPwd,$salt2);
        $save["salt"]=$salt2;
        $result= $model_user->where($where)->save($save);
        if($result) {
            $data_cc['code'] = 200;
            $data_cc['msg'] = "修改成功";
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = "修改失败";
            exit(json_encode($data_cc));
        }
    }
      //更新入口密码
      public function pass_updatec(){
        $uid=UID;
        $model_user=M("user"); 
        $detail = I('post.');
        $aOldPwd = $detail["aOldPwd"];
        $aNewPwd = $detail["aNewPwd"];
        if(empty($aOldPwd)){
            $data['code']=0;
            $data['msg']='请输入旧入口密码';
            exit(json_encode($data));
        }
        if(empty($aNewPwd)){
            $data['code']=0;
            $data['msg']='请输入新密码';
            exit(json_encode($data));
        }
        $where['issh']=1; 
        $where['uid']=$uid; 
        $rs_pwd = $model_user->where($where)->field("pass_word,salts")->find();
        $salt=$rs_pwd['salts'];
        $aOldPwdMd5=$this->getEncryptPassword($aOldPwd,$salt);
        if($aOldPwdMd5 != $rs_pwd["pass_word"]){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "原入口密码错误";
            exit(json_encode($data_cc));
        }
        if($aOldPwd==$aNewPwd){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "原入口密码和新密码一致,无法修改";
            exit(json_encode($data_cc));
        }
        $salt2= $this->GetRandStr(6);
        $save["pass_word"] =$this->getEncryptPassword($aNewPwd,$salt2);
        $save["salts"]=$salt2;
        $result= $model_user->where($where)->save($save);
        if($result) {
            $data_cc['code'] = 200;
            $data_cc['msg'] = "入口密码修改成功";
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = "修改失败";
            exit(json_encode($data_cc));
        }
    }

    //更新系统设置
    public function baseconfig_update(){
        $uid=UID;
        $model_user=M("user_config"); 
        $detail = I('post.');
        $logo = $detail["logo"];
        $names = $detail["names"];
        $bg_img = $detail["bg_img"];
        if(empty($logo)){
            $data['code']=0;
            $data['msg']='请上传LOGO';
            exit(json_encode($data));
        }
        if(empty($names)){
            $data['code']=0;
            $data['msg']='请输入门店名称';
            exit(json_encode($data));
        }
        if(empty($bg_img)){
            $data['code']=0;
            $data['msg']='请上传背景图';
            exit(json_encode($data));
        }
        $where['uid']=$uid; 
        $save['logo']=$logo;
        $save['names']=$names;
        $save['bg_img']=$bg_img;
        $result= $model_user->where($where)->save($save);
        if($result) {
            $data_cc['code'] = 200;
            $data_cc['msg'] = "修改成功";
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = "修改失败";
            exit(json_encode($data_cc));
        }
    }
}